package cn.edu.sjtu.se.dslab.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.edu.sjtu.se.dslab.action.bean.PageBean;
import cn.edu.sjtu.se.dslab.auth.User;
import cn.edu.sjtu.se.dslab.dao.IProjectDao;
import cn.edu.sjtu.se.dslab.exception.DBException;
import cn.edu.sjtu.se.dslab.project.Project;
import cn.edu.sjtu.se.dslab.project.UserProject;
import cn.edu.sjtu.se.dslab.service.IDesignPatternComponentService;
import cn.edu.sjtu.se.dslab.service.IProjectService;
import cn.edu.sjtu.se.dslab.service.IUserProjectService;
import cn.edu.sjtu.se.dslab.util.FileUtil;

@Service("projectService")
@Transactional
public class ProjectServiceImpl implements IProjectService {

	@Resource(name="projectDao")
	IProjectDao projectDao;
	
	@Resource(name="designPatternComponentService")
	IDesignPatternComponentService designPatternComponentService;
	
	@Resource(name="userProjectService")
	IUserProjectService userProjectService;
	
	@Override
	public boolean addProject(Project project) {
		project.setCreateTime(new Date());
		FileUtil.createDirectorys(project.getFolderPath());
		projectDao.save(project);
		//create the four designpattern component
		designPatternComponentService.addAllDesignPatternComponent(project);
		UserProject up = new UserProject();
		up.setAdmin(true);
		up.setProject(project);
		up.setUser(project.getOwner());
		userProjectService.addUserProject(up);
		return true;
	}
	
	@Override
	public boolean addDesignPatternCategory(Project project) {
		project.setCreateTime(new Date());
		FileUtil.createDirectorys(project.getFolderPath());
		projectDao.save(project);
		//create the four designpattern component
		designPatternComponentService.addAllDesignPatternComponent(project);
		return true;
	}

	@Override
	public Project updateProject(Project project) {
		return projectDao.update(project);
	}

	@Override
	public Project getProjectById(long id) {
		return projectDao.queryById(id);
	}

	@Override
	public Project getProjectByUri(String uri) {
		return projectDao.queryByUri(uri);
	}

	@Override
	public PageBean getPageBean(int pageNum) throws DBException {
		return null;
	}

	@Override
	public List<Project> getProjectList() {
		//return projectDao.queryByList();
		return projectDao.queryValidProjects();
	}

	@Override
	public boolean deleteProject(long id) {
		Project project = projectDao.queryById(id);
		if (project.isDeleted()) return false;
		project.setDeleted(true);
		projectDao.update(project);
		return true;
	}

	@Override
	public List<Project> getDesignPatternCategoryByUser(User user) {
		return projectDao.queryDesignPatternCategoryByUserId(user.getId());
	}

	@Override
	public List<Project> getDesignPatternCategoryList() {
		return projectDao.queryDesignPatternCategoryByList();
	}

	@Override
	public List<Project> getDesignPatternCategoryByAttr(int pageNum, String name ,String description, long groupId) {
		return projectDao.queryDesignPatternCategoryByAttr(pageNum, name, description, groupId);
	}

}
